$(document).ready(function() {
	$("#demo").validate({
		rules:{
			"excellentCourse.exceCourseName": {
				required: true,
				maxlength: 30
			},
			"excellentCourse.departmentId": {
				required: true	    		
			},
			"excellentCourse.majorId": {
				required: true	    		
			},
			"excellentCourse.exceCourseMark": {
				required: false,
				maxlength: 500
			},
		
			"excellentCourse.imgSrc":{
				required: false,
	    		maxlength: 100
			},
			"excellentCourse.exceCourseUrl":{
				required: true,
				url: true,
	    		maxlength: 100
			}
		}
	});
});

//返回department的json数据，带被选择数据
function queryDepartmentList(departmentId){
	$.post(
		"../AjaxResultData/listAllDepartment.action",
		function(data){
			$("#departmentId").empty();
			$("#departmentId").append("<option value=''>--未选择--</option>");
			var departmentData = eval("("+data.departmentData+")");  
			for(var i = 0;i<departmentData.length;i++){
				if(departmentId==departmentData[i].departmentId){
					$("#departmentId").append("<option value='"+departmentData[i].departmentId+"' selected='selected'>"+departmentData[i].departmentName+"</option>");
				}else{
					$("#departmentId").append("<option value='"+departmentData[i].departmentId+"'>"+departmentData[i].departmentName+"</option>");
				}
			}
		},
			"json"
	);
}

//根据DepartmentId返回major的json数据
function queryMajorByDepartmentId(departmentId,majorId){
	$.post(
		"../AjaxResultData/listAllMajorByDepartmentId.action",
		{"major.departmentId":departmentId},
		function(data){
			$("#majorId").empty();
			$("#majorId").append("<option value=''>--未选择--</option>");
			var majorData = eval("("+data.majorDataByDepartmentId+")");
			for(var i = 0; i<majorData.length; i++){
				if(majorId==majorData[i].majorId){
					$("#majorId").append("<option value='"+majorData[i].majorId+"' selected='selected'>"+majorData[i].majorName+"</option>");	
				}else{
					$("#majorId").append("<option value='"+majorData[i].majorId+"'>"+majorData[i].majorName+"</option>");
				}
			}
		}
	);
}

function createDepartmentChange(majorId){
	$("#departmentId").change(function(){
		var departmentId = document.getElementById("departmentId").value;
		$("#majorId").empty();
		queryMajorByDepartmentId(departmentId,majorId);
	});
}

function initForm(departmentId,majorId){
	queryDepartmentList(departmentId);
	queryMajorByDepartmentId(departmentId,majorId);
}